/*
 * Copyright 2011 Casey Stehlik, Brad, Tyler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.softeng.network.chess;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainMenu 
{
	public LoadingMenu mLoadingMenu;
	
	public Chess mChess;
	
	public JFrame mWindow;
	
	public String mGameTitle = "Network Chess";
	
	public int mWidth = 400;
	
	public int mHeight = 160;
	
	public JLabel mNameLabel;
	
	public JLabel mIPLabel;
	
	public JLabel mMyIPLabel;
	
	public JLabel mMyIP;
	
	public JTextField mNameTextField;
	
	public JTextField mIPTextField;
	
	public JButton mSendRequestButton;
	
	public JButton mHostRequestButton;
	
	public MainMenu()
	{
		this.mWindow = new JFrame(this.mGameTitle);
		this.mWindow.setSize(new Dimension(this.mWidth ,this.mHeight));

		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		this.mNameTextField = new JTextField();
		this.mNameTextField.setSize(200, 25);
		this.mNameTextField.setLocation(140, 10);
		panel.add(this.mNameTextField);
		
		this.mIPTextField = new JTextField();
		this.mIPTextField.setSize(200, 25);
		this.mIPTextField.setLocation(140, 40);
		panel.add(this.mIPTextField);
		
		this.mSendRequestButton = new JButton("Send Game Request");
		this.mSendRequestButton.setSize(150,25);
		this.mSendRequestButton.setLocation(50, 100);
		panel.add(this.mSendRequestButton);
		this.mSendRequestButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				if(!mNameTextField.getText().equals(""))
				{
					if(!mIPTextField.getText().equals(""))
					{
						if(validateIPRequest(mIPTextField.getText()))
						{
							System.out.println("Start Chess with Request");
							System.out.println("Username: " + mNameTextField.getText());
							System.out.println("Request Address: " + mIPTextField.getText());
							
							Chess.getInstance(false).display();
							mWindow.setVisible(false);
						}
						else
						{
							System.out.println("Invalid IP Address Request");
						}
					}
					else
					{
						System.out.println("Please enter an address");
					}
				}	
				else
				{
					System.out.println("Please enter a username");
				}
			}
		});
		
		this.mHostRequestButton = new JButton("Host a Game");
		this.mHostRequestButton.setSize(130,25);
		this.mHostRequestButton.setLocation(210, 100);
		panel.add(this.mHostRequestButton);
		this.mHostRequestButton.addActionListener(new ActionListener()
		{	
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				if(!mNameTextField.getText().equals(""))
				{
					System.out.println("Start Chess with Hosting");
					System.out.println("Username: " + mNameTextField.getText());
					
					new Server();
					
					Chess.getInstance(true).display();
					mWindow.setVisible(false);
				}
				else
				{
					System.out.println("Please enter a username");
				}
			}
		});
		
		this.mNameLabel = new JLabel("Username");
		this.mNameLabel.setSize(100, 25);
		this.mNameLabel.setLocation(50, 10);
		panel.add(this.mNameLabel);
		
		this.mIPLabel = new JLabel("Address");
		this.mIPLabel.setSize(100, 25);
		this.mIPLabel.setLocation(50, 40);
		panel.add(this.mIPLabel);
		
		this.mMyIPLabel = new JLabel("My Address");
		this.mMyIPLabel.setSize(100, 25);
		this.mMyIPLabel.setLocation(50, 70);
		panel.add(this.mMyIPLabel);
			
		this.mMyIP = new JLabel(this.getIpAddress());
		this.mMyIP.setSize(200, 25);
		this.mMyIP.setLocation(140, 70);
		panel.add(this.mMyIP);
		
		this.mWindow.setContentPane(panel);
		
		this.mWindow.setResizable(false);
	}
	
	public void display()
	{
		this.mWindow.setVisible(true);
	}
	
	private String getIpAddress()
	{
		String addString = "";
		
		try 
		{
			addString = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e)
		{
			e.printStackTrace();
		} 
		
		return addString;
	}
	
	private boolean validateIPRequest(String addr)
	{
		return true;
	}
}
